package com.lzf.filter;

import com.lzf.handler.MyAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CheckClientParameterFilter extends BasicAuthenticationFilter {

    private AuthenticationEntryPoint authenticationEntryPoint;

    public CheckClientParameterFilter(AuthenticationManager authenticationManager) {
        super(authenticationManager);
    }

    public CheckClientParameterFilter(AuthenticationManager authenticationManager, AuthenticationEntryPoint authenticationEntryPoint) {
        super(authenticationManager, authenticationEntryPoint);
        this.authenticationEntryPoint = authenticationEntryPoint;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String url = request.getRequestURI();
        String client_id = request.getParameter("client_id");
        String client_secret = request.getParameter("client_secret");
        if (!StringUtils.isEmpty(url) && StringUtils.pathEquals("/oauth/token", url)) {
            if (StringUtils.isEmpty(client_id)) {
                authenticationEntryPoint.commence(request, response, new BadCredentialsException("clientId为空"));
                return;
            }
            if (StringUtils.isEmpty(client_secret)) {
                authenticationEntryPoint.commence(request, response, new BadCredentialsException("client_secret为空"));
                return;
            }
        }
        String grant_type = request.getParameter("grant_type");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String refresh_token = request.getParameter("refresh_token");
        filterChain.doFilter(request,response);
    }

}
